﻿using System;

namespace SilenthinkContract.Domain
{
    public class UserTrade : BaseEntity<string>
    {
        /// <summary>
        /// 用户Id
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 交易类型 0为购物 1为竞购 2为代充 代付等
        /// </summary>
        public int Category { get; set; }

        /// <summary>
        /// 资金流 1为收入 0为支出
        /// </summary>
        public byte FundFlow { get; set; }

        /// <summary>
        /// 交易总金额(需要支付的总金额)
        /// </summary>
        public decimal Amount { get; set; }

        /// <summary>
        /// 已支付金额(一个交易可能分多次支付)
        /// </summary>
        public decimal Paid { get; set; }

        /// <summary>
        /// 退款金额
        /// </summary>
        public decimal Refunded { get; set; }

        /// <summary>
        /// 代充 代付等用户Id
        /// </summary>
        public int ForUserId { get; set; }

        /// <summary>
        /// 代充 代付等用户名
        /// </summary>
        public string ForUserName { get; set; }

        /// <summary>
        /// 辅助编号 线下用现金或刷卡付款
        /// </summary>
        public string AssistNo { get; set; }

        /// <summary>
        /// 扩展信息
        /// </summary>
        public string Extra { get; set; }

        /// <summary>
        /// 交易状态
        /// </summary>
        public UserTradeStatus Status { get; set; }

        public PayTrade ToPayTrade(string ipAddress)
        {
            var now = DateTime.Now;
            //var prefix = Id.Substring(0, 1);
            var trade = new PayTrade();
            var id = now.ToString("yyMMddHHmmssffff");

            var amount = Amount - Paid;
            trade.Id = id;
            trade.UserId = UserId;
            trade.Category = 2;
            trade.AssociatedId = Id;
            trade.Amount = amount;
            trade.Balance = amount;
            trade.UseBalance = true;
            trade.PaymentCategory = 0;

            trade.Gateway = string.Empty;
            trade.BankName = string.Empty;
            trade.PaymentEBankId = 0;
            trade.PaymentPlatformId = 0;

            trade.CardNo = string.Empty;
            trade.CardPwd = string.Empty;
            trade.CardAmount = string.Empty;

            trade.IpAddress = ipAddress;
            trade.ThirdPartyNo = string.Empty;
            trade.Extra = string.Empty;
            trade.Status = 1;
            trade.CreatedBy = CreatedBy;
            trade.CreatedOn = CreatedOn;

            return trade;
        }

        public PayTrade ToPayTrade(PaymentEBank eBank, string ipAddress)
        {
            var trade = new PayTrade();
            var now = DateTime.Now;
            //var prefix = Id.Substring(0, 1);

            var id =  now.ToString("yyMMddHHmmssffff");

            trade.Id = id;

            if (eBank != null)
            {
                trade.Id = id;
                trade.UserId = UserId;
                trade.Category = 0;
                trade.AssociatedId = Id;
                trade.Amount = Amount;
                trade.Balance = 0;
                trade.UseBalance = false;
                trade.PaymentCategory = 1;
                trade.Gateway = eBank.GatewayCode;
                trade.BankName = eBank.PaymentBank.Name;
                trade.PaymentEBankId = eBank.Id;
                trade.PaymentPlatformId = eBank.PlatformId;
                trade.CardNo = string.Empty;
                trade.CardPwd = string.Empty;
                trade.CardAmount = string.Empty;
                trade.IpAddress = ipAddress;
                trade.ThirdPartyNo = string.Empty;
                trade.Extra = string.Empty;
                trade.Status = 1;
                trade.CreatedBy = CreatedBy;
                trade.CreatedOn = CreatedOn;
            }
            return trade;
        }

    }

    public enum UserTradeStatus : byte
    {
        /// <summary>
        /// 待支付
        /// </summary>
        Pending = 0,
        /// <summary>
        /// 已支付待进一步处理（使用电话卡机充值）
        /// </summary>
        Processing = 1,
        /// <summary>
        /// 已完成
        /// </summary>
        Complete = 2,
        /// <summary>
        /// 已取消
        /// </summary>
        Cancelled = 3,
        /// <summary>
        /// 已过期
        /// </summary>
        Expired = 4,
        /// <summary>
        /// 已删除
        /// </summary>
        Deleted
    }
}